#region Copyright (C) 2005-2010 Team MediaPortal

// Copyright (C) 2005-2010 Team MediaPortal
// http://www.team-mediaportal.com
// 
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.

#endregion
using System;
using System.Collections.Generic;
using System.IO;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using dlCentral.GUI.Items;
using dlCentral.Localizations;

namespace dlCentral.GUI
{
  

  public class GUIDialogStatus : GUIDialogWindow
  {
//    [SkinControl(2)] protected GUIButtonControl btnNo = null;
//    [SkinControl(1)] protected GUIButtonControl btnClose = null;

    [SkinControl(221)] protected GUIProgressControl progress1 = null;
    [SkinControl(222)] protected GUIProgressControl progress2 = null;
    [SkinControl(223)] protected GUIProgressControl progress3 = null;
    [SkinControl(224)] protected GUIProgressControl progress4 = null;
    [SkinControl(225)] protected GUIProgressControl progress5 = null;
    [SkinControl(251)] protected GUILabelControl pTitle1 = null;
    [SkinControl(252)] protected GUILabelControl pTitle2 = null;
    [SkinControl(253)] protected GUILabelControl pTitle3 = null;
    [SkinControl(254)] protected GUILabelControl pTitle4 = null;
    [SkinControl(255)] protected GUILabelControl pTitle5 = null;
    [SkinControl(451)] protected GUILabelControl pInfo1 = null;
    [SkinControl(452)] protected GUILabelControl pInfo2 = null;
    [SkinControl(453)] protected GUILabelControl pInfo3 = null;
    [SkinControl(454)] protected GUILabelControl pInfo4 = null;
    [SkinControl(455)] protected GUILabelControl pInfo5 = null;
    [SkinControl(301)] protected GUIImage statusDialog = null;

    List<StatusItem> _stlist = new List<StatusItem>();
    private bool IsIOError = false;
    private bool firstTime = true;
    private int checkCounter = 0;
    private bool m_bConfirmed = false;
    private DateTime timeStart = DateTime.Now;

    public GUIDialogStatus()
    {
      GetID = 28111966;
    }

    /// <summary>
    /// MediaPortal will set #currentmodule with GetModuleName()
    /// </summary>
    /// <returns>Localized Window Name</returns>
    public override string GetModuleName()
    {
        return Localization.Status;
    }

    public override bool Init()
    {
        return Load(GUIGraphicsContext.Skin + @"\dlCentral.Status.xml");
    }

 

    public override void DoModal(int dwParentId)
    {
        AllocResources();
        InitControls();
        timeStart = DateTime.Now;
        pTitle1.Label = string.Empty;
        pTitle2.Label = string.Empty;
        pTitle3.Label = string.Empty;
        pTitle4.Label = string.Empty;
        pTitle5.Label = string.Empty;
        pInfo1.Label = string.Empty;
        pInfo2.Label = string.Empty;
        pInfo3.Label = string.Empty;
        pInfo4.Label = string.Empty;
        pInfo5.Label = string.Empty;
        progress1.Visible = false;
        progress2.Visible = false;
        progress3.Visible = false;
        progress4.Visible = false;
        progress5.Visible = false;
        base.DoModal(dwParentId);
        

    }

    public override bool ProcessDoModal()
    {
        base.ProcessDoModal();

        TimeSpan timeElapsed = DateTime.Now - timeStart;
        if (!firstTime)
        if (timeElapsed.TotalSeconds < 10)
        {
            return true;
        }
        timeStart = DateTime.Now;
        _stlist.Clear();
        _stlist = GetStatusList();

        if (_stlist.Count == 0)
        {
            if (!IsIOError || checkCounter>2) PageDestroy();
            return true;
        }
        firstTime = false;
        checkCounter = 0;
        int itemCounter = 0;
        foreach (StatusItem si in _stlist)
        {
            if (itemCounter < 5)
            {
                string s = si.Info;
                SetInfo(itemCounter, Convert.ToInt32(si.Progress), si.Title, s);
                itemCounter++;
            }

        }
        if (itemCounter < 2 && progress2.Visible)
        { 
            pTitle2.Label = string.Empty;
            pInfo2.Label = string.Empty;
            progress2.Visible = false;
        }
        if (itemCounter < 3 && progress3.Visible)
        {
            pTitle3.Label = string.Empty;
            pInfo3.Label = string.Empty;
            progress3.Visible = false;
        }
        if (itemCounter < 4 && progress4.Visible)
        {
            pTitle4.Label = string.Empty;
            pInfo4.Label = string.Empty;
            progress4.Visible = false;
        }
        if (itemCounter < 5 && progress5.Visible)
        {
            pTitle5.Label = string.Empty;
            pInfo5.Label = string.Empty;
            progress5.Visible = false;
        }
        
        return true;
    }


    public void SetInfo( int Index, int perc, string title, string info)
    {
//        AllocResources();
//        InitControls();

        switch (Index)
        {
            case 0:
                progress1.Percentage = perc;
                pTitle1.Label = title;
                pInfo1.Label = info;
                if (!progress1.Visible) 
                 progress1.Visible=true;
                break;
            case 1:
                progress2.Percentage = perc;
                pTitle2.Label = title;
                pInfo2.Label = info;
                if (!progress2.Visible) 
                 progress2.Visible=true;
                break;
            case 2:
                progress3.Percentage = perc;
                pTitle3.Label = title;
                pInfo3.Label = info;
                if (!progress3.Visible) 
                 progress3.Visible=true;
                break;
            case 3:
                progress4.Percentage = perc;
                pTitle4.Label = title;
                pInfo4.Label = info;
                if (!progress4.Visible) 
                 progress4.Visible=true;
                break;
            case 4:
                progress5.Percentage = perc;
                pTitle5.Label = title;
                pInfo5.Label = info;
                if (!progress5.Visible) 
                 progress5.Visible=true;
                break;
        }
    }

    public List<StatusItem> GetStatusList()
    {
        List<StatusItem> res1 = new List<StatusItem>();

        string CommPath = Settings.SettingsManager.Properties.GeneralSettings.CommPath + "\\";
        try
        {
            IsIOError = false;

            if (File.Exists(CommPath + "status.txt"))
            {
                FileInfo f = new FileInfo(CommPath + "status.txt");
                long i2 = f.Length;
                if (i2 > 0)
                {
                 List<string> res = new List<string>();
                 res.AddRange(File.ReadAllLines(CommPath + "status.txt"));
                 foreach (string str1 in res)
                 {
                     StatusItem si = new StatusItem();
                     string[] s1 = str1.Split('|');
                     si.Title = s1[0];
                     si.Progress = s1[1];
                     si.Info = s1[1] + "%  " + s1[2];
                     res1.Add(si);
                 }
                }

            }
            else checkCounter++;
        }
        catch (IOException e)
        {
            checkCounter = 0;
            IsIOError=true;
            Log.Info("IOError {0}", e.Message);
            res1.Clear();
            return res1;
        }

        return res1;
    }

    public bool IsConfirmed
    {
      get { return m_bConfirmed; }
    }

    public void SetHeading(string strLine)
    {
      //LoadSkin();
//      AllocResources();
//      InitControls();

      SetControlLabel(GetID, 1, strLine);

      SetLine(1, string.Empty);
      SetLine(2, string.Empty);
      SetLine(3, string.Empty);
      SetLine(4, string.Empty);
    }

    public void SetHeading(int iString)
    {
      if (iString == 0)
      {
        SetHeading(string.Empty);
      }
      else
      {
        SetHeading(GUILocalizeStrings.Get(iString));
      }
    }

    public void SetLine(int iLine, string strLine)
    {
      if (iLine <= 0)
      {
        return;
      }
      GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_LABEL_SET, GetID, 0, 1 + iLine, 0, 0, null);
      msg.Label = strLine;
      OnMessage(msg);
    }

    public void SetLine(int iLine, int iString)
    {
      if (iLine <= 0)
      {
        return;
      }
      if (iString == 0)
      {
        SetLine(iLine, string.Empty);
      }
      SetLine(iLine, GUILocalizeStrings.Get(iString));
    }
  }
}